<?php
class Dbmodel extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		
    }
	function get_last_entries( $table, $rows, $order_by, $field='')
    {
		if ( $field != '' )
			$this->db->select($field);
		$this->db->order_by($order_by);
        $query = $this->db->get($table, $rows);
        return $query->result();
    }
    function insert_entry($table, $data)
    {


        $this->db->insert($table, $data);
    }
	function update_entry($table, $cond, $data)
    {

		$this->db->where($cond);
        $this->db->update($table, $data);
    }
	function select_where($table, $cond='', $field='', $join='')
	{
		if ( $field != '' )
			$this->db->select($field);
		if ( $join != '' )
			$this->db->join($join);
		if ( $cond != '' )
			$this->db->where($cond); 
		
		$query = $this->db->get($table);
	    
		return $query->result();
	}

	function select_not_in($table, $f, $not_in, $cond='', $field='', $join='')
	{
		if ( $field != '' )
			$this->db->select($field);

		if ( $join != '' )
			$this->db->join($join);

		if ( $cond != '' )
			$this->db->or_where($cond); 

		if ( $f != '' )
			$this->db->where_not_in($f, $not_in); 
		
		$query = $this->db->get($table);
	    
		return $query->result();
	}

	function select_like($table, $like, $or_like, $field='', $join='', $order_by='', $sum='', $group='')
	{



		if ( $field != '' )
			$this->db->select($field);
		else
			$this->db->select('*');

		if ( $sum != '' )
			$this->db->select_sum($sum);

		if ( $join != '' )
		{
			$join = explode(',', $join);
			$this->db->join($join[0], $join[1]);
		}

		$this->db->like($like);
		$this->db->or_like($or_like); 
		
		if ( $group != '' )
			$this->db->group_by($group);

		if ( $order_by != '' )
			$this->db->order_by($order_by);

		$query = $this->db->get($table);
		return $query->result();
	}

	function select_where2($table, $cond, $field='', $join='')
	{
		
		

		if ( $field != '' )
			$this->db->select($field);
		else
			$this->db->select('*');
		if ( $join != '' )
			$this->db->join($join);

		$temp = explode(':', $cond);

		
		for($i=0; $i<count($temp); $i++)
		{
		
			$this->db->where($temp[$i]);

	
		}

		
		

		$query = $this->db->get($table);
		return $query->result();
	}

	function select_or_where($table, $cond1, $cond2, $field='', $join='')
	{
		

		if ( $field != '' )
			$this->db->select($field);
		else
			$this->db->select('*');
		if ( $join != '' )
			$this->db->join($join);

		$this->db->where($cond1);
		$this->db->or_where($cond2); 

		$query = $this->db->get($table);
		return $query->result();
	}

	function select_join($table, $table2, $field='', $join='', $cond='', $order_by='')
	{
		if ( $field != '' )
			$this->db->select($field);
		else
			$this->db->select('*');
		
		$this->db->from($table);

		if ( $cond != '' )
			$this->db->where($cond);

		$this->db->join($table2, $join);

		if ( $order_by != '' )
			$this->db->order_by($order_by);

		$query = $this->db->get();

	    
		return $query->result();
	}

	function multi_join($table, $field='', $join, $cond='', $order_by='', $like='', $or_like='')
	{
		

		if ( $field != '' )
			$this->db->select($field);
		else
			$this->db->select('*');
		
		$this->db->from($table);

	

		$temp = explode(',', $join);
		for($i=0; $i<count($temp); $i++)
		{
			$temp_join = explode(':', $temp[$i]);
			$joins[$i]['table'] = $temp_join[0];
			$joins[$i]['join'] = $temp_join[1];

			$this->db->join($joins[$i]['table'], $joins[$i]['join']);
		}

		if ( $cond != '' )
		{
			$this->db->where($cond);
			
		}

//		$this->db->join($table2, $join);
		if( $like != '' )
			$this->db->like($like);

		if( $or_like != '' )
			$this->db->or_like($or_like); 

		if ( $order_by != '' )
			$this->db->order_by($order_by);



		$query = $this->db->get();

	

		return $query->result();
	}

	function delete_entry($table, $field, $value)
	{
		$this->db->where($field, $value);
		$this->db->delete($table); 
	}

}

?>